package com.youth.mapper;

import com.youth.entity.po.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @author: wuxian
 * @date: 2020/1/6 17:32
 */
@Mapper
public interface UserMapper {

    /**
     * 根据用户名获取用户信息
     *
     * @param userName
     * @return
     */
    @Select("select * from t_user where username = #{userName}")
    User getUserInfoByName(String userName);

    /**
     * 根据用户名获取用户角色
     *
     * @param userName
     * @return
     */
    @Select("SELECT\n" +
            "\tc.role\n" +
            "FROM\n" +
            "\t`user`.t_user AS a\n" +
            "INNER JOIN `user`.t_user_role AS b ON a.id = b.uid\n" +
            "INNER JOIN `user`.t_role AS c ON b.role_id = c.id\n" +
            "WHERE\n" +
            "\ta.username = #{userName}")
    List<String> getUserRoleByName(String userName);

    /**
     * 根据用户名获取用户权限
     *
     * @param userName
     * @return
     */
    @Select("SELECT\n" +
            "e.`name`\n" +
            "FROM\n" +
            "`user`.t_user AS a\n" +
            "INNER JOIN `user`.t_user_role AS b ON a.id = b.uid\n" +
            "INNER JOIN `user`.t_role AS c ON b.role_id = c.id\n" +
            "INNER JOIN `user`.t_role_permission AS d ON c.id = d.role_id\n" +
            "INNER JOIN `user`.t_permission AS e ON d.permission_id = e.id\n" +
            "WHERE\n" +
            "a.username = #{userName}")
    List<String> getUserPressionByName(String userName);

    @Insert("<script>" +
            "    insert into t_user\n" +
            "    <trim prefix=\"(\" suffix=\")\" suffixOverrides=\",\" >\n" +
            "      <if test=\"id != null\" >\n" +
            "        id,\n" +
            "      </if>\n" +
            "      <if test=\"username != null\" >\n" +
            "        username,\n" +
            "      </if>\n" +
            "      <if test=\"password != null\" >\n" +
            "        password,\n" +
            "      </if>\n" +
            "      <if test=\"email !=null\">\n" +
            "        email,\n" +
            "      </if>\n" +
            "      <if test=\"roleId != null\" >\n" +
            "        role_id,\n" +
            "      </if>\n" +
            "    </trim>\n" +
            "    <trim prefix=\"values (\" suffix=\")\" suffixOverrides=\",\" >\n" +
            "      <if test=\"id != null\" >\n" +
            "        #{id,jdbcType=INTEGER},\n" +
            "      </if>\n" +
            "      <if test=\"username != null\" >\n" +
            "        #{username,jdbcType=VARCHAR},\n" +
            "      </if>\n" +
            "      <if test=\"password != null\" >\n" +
            "        #{password,jdbcType=VARCHAR},\n" +
            "      </if>\n" +
            "      <if test=\"email !=null\">\n" +
            "        #{email},\n" +
            "      </if>\n" +
            "      <if test=\"roleId != null\" >\n" +
            "        #{roleId,jdbcType=INTEGER},\n" +
            "      </if>\n" +
            "    </trim>\n" +
            "</script>")
    int insertUser(User user);
}
